/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package PictureDescription;

import java.awt.*;
import java.io.*;
import javax.swing.*;
import GuiTools.*;

/**
 * A panel displaying an PictureUnit's image indicating activity during the image loading process.
 * @author Christoph Schnack
 *
 */
public class PreviewPanel extends JPanel{
	Image image;
	SwingWorker ilt;
	LoadingThread lt;
	boolean loading,up=true;
	int pos = 55;
	public PreviewPanel(String title){
		super();
		this.setPreferredSize(new Dimension(200,200));
		this.setBorder(BorderFactory.createTitledBorder(title));
		repaint();
	}
	
	public void setImage(String coll,String pu){
		if(lt!=null &&lt.getState()==SwingWorker.StateValue.STARTED){
			lt.cancel();
		}
		lt = new LoadingThread(coll,pu);
		lt.execute();
		this.repaint();
	}
	
	public void setNoImage(){
		image = null;
		this.repaint();
	}
	
	public void paintComponent(Graphics g){
		g.setColor(Color.lightGray);
		g.fillRect(0,0,200,200);
		if(loading){
			g.setColor(Color.black);
			g.drawString("LOADING", 80, 70);
			pos+=up?5:-5;
			g.fillRect(pos, 100, 10, 10);
			if(pos==150){
				up  = false;
			}
			if(pos == 50){
				up = true;
			}
		}
		else{
			Graphics2D g2 = (Graphics2D) g;
			if(image!=null)g2.drawImage(image, 25, 25, 150, 150, this);
		}
	}
	
	class LoadingThread extends SwingWorker<String, Object>{
		String coll,pu;
		public LoadingThread(String c, String p){
			coll = c;
			pu = p;
		}
		public String doInBackground() throws InterruptedException{
			ilt = new ImageLoadingThread(coll,pu);
			ilt.execute();
			loading = true;
			while(!ilt.isDone()){
				repaint();
				Thread.sleep(100);
			}
			loading = false;
			return "";
		}
		
		public void done(){
			repaint();
		}
		
		public void cancel(){
			if(ilt!=null)ilt.cancel(true);
			this.cancel(true);
			loading=false;
		}
	}
	
	class ImageLoadingThread extends SwingWorker<Image, Object>{
		String coll,pu;
		public ImageLoadingThread(String c, String p){
			coll = c;
			pu = p;
		}
		public Image doInBackground(){
			return GuiTools.loadImage(GuiTools.PATHTOPICTUREMAPS+File.separator+coll+File.separator+pu+File.separator+"image");
		}
		
		public void done(){
			if(!this.isCancelled()){
				try{
					image = get();
				}
				catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
}